package com.hnichr.stop.message.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.share.WhitelistAddOrDeleteShare;
import com.hnichr.stop.message.MessageArrivedHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class WhiteListAddOrDeleteMessageArrivedHandle implements MessageArrivedHandle {
    private static final Logger logger = LoggerFactory.getLogger(WhiteListAddOrDeleteMessageArrivedHandle.class);

    @Override
    public boolean support(String topic, String message) {
        if(!"b956ba98-988cd83e/device/message/down/white_list_operator/reply".equals(topic)){
            return false;
        }
        JSONObject jsonObject = JSON.parseObject(message);
        return jsonObject.containsKey("payload")&&jsonObject.get("payload")==null;
    }

    @Override
    public void handle(String topic, String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            String code = jsonObject.get("code").toString();
            synchronized (WhitelistAddOrDeleteShare.getInstance()){
                WhitelistAddOrDeleteShare.getInstance().setCode(code);
                WhitelistAddOrDeleteShare.getInstance().notifyAll();
            }
        }
        catch (Exception e){
            logger.error("WhiteListAddOrDeleteMessageArrivedHandle报错",e);
        }


    }
}
